﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMS.MainClasses;
using GMS.ServiceFunctions.FunctionForControls;
using GMS.ServiceFunctions.Data;
using GMS.MainForm;


namespace GMS.Groups
{
    public partial class ViewGroups : ChildForms
    {
        FunctionsForDataGridView FFDGV = new FunctionsForDataGridView();
        BindingSource BS = new BindingSource();
        public ViewGroups()
        {
            InitializeComponent();
            StoredProcedures = new String[2];
            StoredProcedures[0] = "[SelectGroups]";
            StoredProcedures[1] = "[DeactivateGroup]";
            

            dataGridView1.DataSource = BS;
            
        }
        private void RefreshGrid()
        {
            BS.DataSource = MRF.ReturnDataSetDB(ConnectionString, StoredProcedures[0]);
            BS.DataMember = "table";
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            FFDGV.TextForGroupBox(groupBox1, dataGridView1, "Список групп              ");
        }

        private void AddGroupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddEditGroups AEG = new AddEditGroups(AddEditGroups.EditMode.Добавление);
            AEG.ConnectionString = ConnectionString;
            AEG.MdiParent = MRF;
            AEG.Show();
        }

        private void EditGroupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddEditGroups AEG = new AddEditGroups(AddEditGroups.EditMode.Редактирование);
            AEG.ConnectionString = ConnectionString;
            AEG.MdiParent = MRF;
            AEG.IdGroup = Convert.ToInt32(dataGridView1.CurrentRow.Cells["IdGroup"].Value);
            AEG.Show();
        }

        private void DeleteGroupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Удаление группы, вы уверены?", "Предупреждение!!!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                AddEditGroups AEG = new AddEditGroups(AddEditGroups.EditMode.Удаление);
                AEG.ConnectionString = ConnectionString;
                AEG.MdiParent = MRF;
                AEG.IdGroup = Convert.ToInt32(dataGridView1.CurrentRow.Cells["IdGroup"].Value);
                AEG.Show();
            }
        }

        private void ViewGroups_Activated(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void deactivateGroupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MRF.ExecuteSqlCommand(StoredProcedures[1] + " " + Convert.ToInt32(dataGridView1.CurrentRow.Cells["IdGroup"].Value), ConnectionString);
            RefreshGrid();
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            BS.Filter = "[Название группы] like '%"+textBox1.Text.Trim()+"%'";
        }

        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
            BS.Filter = "[Описание группы] like '%"+textBox2.Text.Trim()+"%'";
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            FFDGV.PaintGridRows(dataGridView1, e, dataGridView1.Columns["GroupStatus"].Index, -1, System.Drawing.Color.Red);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            AddGroupToolStripMenuItem_Click(sender, e);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            EditGroupToolStripMenuItem_Click(sender, e);
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            deactivateGroupToolStripMenuItem_Click(sender, e);
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            DeleteGroupToolStripMenuItem_Click(sender, e);
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            GMS.Groups.AdoutGroups AG = new AdoutGroups();
            AG.Show();
        }
    }
}
